﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Zhaoxi.SmartParking.Client.BaseModule.Models;
using Zhaoxi.SmartParking.Client.IBLL;

namespace Zhaoxi.SmartParking.Client.BaseModule.ViewModels
{
    public class ModifyUserDialogViewModel : BindableBase, IDialogAware
    {
        public string Title => "用户信息编辑";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog() => true;

        public void OnDialogClosed() { }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            // 接收编辑状态：新增/编辑
            // 获取要编辑的用户信息
            MainModel = parameters.GetValue<UserModel>("model");
        }

        public UserModel MainModel
        {
            get => _mainModel;
            set { SetProperty<UserModel>(ref _mainModel, value); }
        }
        private UserModel _mainModel = new UserModel();

        // 确认
        public ICommand ConfirmCommand
        {
            get => new DelegateCommand(() =>
            {
                _userBLL.SaveUser(new Entity.UserEntity
                {
                    UserId = MainModel.UserId,
                    UserName = MainModel.UserName,
                    RealName = MainModel.RealName,
                    //UserIcon = MainModel.UserIcon?.Replace(System.Configuration.ConfigurationManager.AppSettings["api_domain"].ToString(), ""),
                    Password = MainModel.Password,
                    Age = MainModel.Age
                });

                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            });
        }
        // 取消
        public ICommand CancelCommand
        {
            get => new DelegateCommand(() =>
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });
        }

        IUserBLL _userBLL;
        public ModifyUserDialogViewModel(IUserBLL userBLL)
        {
            _userBLL = userBLL;
        }
    }
}
